'use strict';

const Controller = require('egg').Controller;
let jwt = require("jsonwebtoken")

class HomeController extends Controller {

    //楼房列表 求租 求购
    async hotRoom() {
        const { ctx, app } = this
        const data = await app.mysql.select('hotRoom')
        const { authorization } = ctx.request.header

        try {
            let res = jwt.verify(authorization, '1901A')
            ctx.body = {
                code: 1,
                msg: "成功",
                res,
                data
            }
        } catch (error) {
            ctx.body = {
                code: 0,
                msg: '登录失效，请重新登录'
            }
        }
    }


    //获取房源详情数据
    async Detail() {
        const { app, ctx } = this
        const { id } = ctx.request.body
        const data = await app.mysql.get('hotRoom', { id })

        ctx.body = {
            code: 1,
            data
        }
    }

    // 房源户型 筛选
    async screening() {
        const { app, ctx } = this
        const { type, orientation } = ctx.request.body
        //const data = await app.mysql.select("hotRoom", { where: { type: value } })
        const sql = {};
        type && (sql.type = type);
        orientation && (sql.orientation = orientation);
        console.log(sql);

        const results = await this.app.mysql.select('hotRoom', { // 搜索 post 表
            where: sql, // WHERE 条件
        });
        ctx.body = {
            code: 1,
            data: results,
        }
    }



    //  求租  求购
    async screening2() {
        const { app, ctx } = this
        const { type2, address } = ctx.request.body
        //const data = await app.mysql.select("hotRoom", { where: { type: value } })
        const sql = {};
        type2 && (sql.type2 = type2);
        address && (sql.address = address);
        console.log(sql);

        const results = await this.app.mysql.select('hotRoom', { // 搜索 post 表
            where: sql, // WHERE 条件
        });
        ctx.body = {
            code: 1,
            data: results,
        }
    }



    //楼市圈
    async housingRing() {
        const { ctx, app } = this
        const data = await app.mysql.select('housingRing')
        const { authorization } = ctx.request.header

        try {
            let res = jwt.verify(authorization, '1901A')
            ctx.body = {
                code: 1,
                msg: "成功",
                res,
                data
            }
        } catch (error) {
            ctx.body = {
                code: 0,
                msg: '登录失效，请重新登录'
            }
        }
    }



    //楼市圈删除数据
    async housingRingDel() {
        const { app, ctx } = this
        const { id } = ctx.request.body
        const data = await app.mysql.delete('housingring', { id })

        ctx.body = {
            code: 1,
            data
        }
    }


    //消息列表 经纪人  排序列表
    async newsList() {
        const { ctx, app } = this
        const data = await app.mysql.select('newsList')
        const { authorization } = ctx.request.header

        try {
            let res = jwt.verify(authorization, '1901A')
            ctx.body = {
                code: 1,
                msg: "成功",
                res,
                data
            }
        } catch (error) {
            ctx.body = {
                code: 0,
                msg: '登录失效，请重新登录'
            }
        }
    }



    //排行排序搜索
    async rankingsearch() {
        const { app, ctx } = this
        const { value } = ctx.request.body
        const data = await app.mysql.query(`select * from newsList where name like "%${value}%"`)

        ctx.body = {
            code: 1,
            data
        }
    }

    //消息删除数据
    async newsDel() {
        const { app, ctx } = this
        const { id } = ctx.request.body
        const data = await app.mysql.delete('newsList', { id })

        ctx.body = {
            code: 1,
            data
        }
    }




  //渲染直播列表数据
  async liveList() {
    const { ctx, app } = this
    const data = await app.mysql.select('liveList')
    const { authorization } = ctx.request.header
    try {
      let res = jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        res,
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }

  //渲染团购列表数据
  async tableData2() {
    const { ctx, app } = this
    const data = await app.mysql.select('tableData2')
    const { authorization } = ctx.request.header
    try {
      let res = jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        res,
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }


}

module.exports = HomeController;
